package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Description AUTO
 * @ClassName UserInfoController
 * @Author ZhengXinjie
 * @date 2021.02.10 13:56
 */
@RestController
@RequestMapping("user")
public class UserInfoController {

    private final UserInfoService userInfoService;

    @Autowired
    public UserInfoController(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    /**
     * 完善个人用户信息
     * @param param 前端传递的用户信息
     * @param token 用户登录的token
     * @return 浏览器响应结果
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String, String> param,
                                               @RequestHeader("Authorization") String token){

        try {
            boolean b = userInfoService.saveUserInfo(param, token);
            if(b){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存用户信息失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    @RequestMapping("loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(@RequestHeader("Authorization") String token,
                                               @RequestParam("headPhoto") MultipartFile file){
        try {
            boolean b = userInfoService.saveUserLogo(file, token);
            if(b){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存用户logo失败！").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}

